<template>
	<view class="addressContainer">
		<view class="scrollView">
			<scroll-view class="scrollViewScroll" scroll-y="true" style="height: 100%;" >
				<view v-for="(item,index) in list" :key="index" class="items" @click="goBack(item)" v-if="list.length>0">
					<view class="text">{{item.name}} {{item.phone}}</view>
					<view class="text">{{item.province+item.city+item.district+item.detail}}</view>
					<view class="editBtn">
						<view class="edit" @click="editAddress(item)">编辑</view>
						<view class="remove" @click="removeAddress(item)">删除</view>
					</view>
				</view>
				<view v-else class="kong">
					<img class="img" style="width: 50px;height: 50px;" src="@/static/kong.png" alt="">
					<view>请先添加地址~</view>
				</view>
			</scroll-view>
		</view>
		<view class="addAddress">
			<view class="addButton" @click="toAdd">添加地址</view>
		</view>
	</view>
</template>

<script>
	import { userAddress, addressDelete } from '@/api/home.js'
	export default {
		data() {
			return {
				list:[],
				goBackType: 0
			}
		},
		onLoad(options) {
			this.goBackType = options.goBack
		},
		onShow() {
			userAddress().then(res=>{
				this.list = res.data
			})
		},
		methods:{
			goBack(data){
				if(this.goBackType == 1) {
					uni.$emit('getAddress', data)
					uni.navigateBack({delta:1})
				}
			},
			toAdd(){
				uni.navigateTo({
					url:'/pages/addAddress/index'
				})
			},
			editAddress(item){
				let params = JSON.stringify(item)
				uni.redirectTo({
					url: '/pages/addAddress/index?item='+params
				})
			},
			removeAddress(item){
				let that = this
				uni.showModal({
					content:'确定删除该地址吗？',
					confirmText:'确定',
					success(data) {
						if(data.confirm){
							addressDelete({id:item.id}).then(res=>{
								userAddress().then(res=>{
									that.list = res.data
								})
							})
						}
					}
				})
			}
		}
	}
</script>

<style scoped lang="less">
	// 隐藏滚动条 vue3写法
	::v-deep ::-webkit-scrollbar {
		display: none;
		width: 0;
		height: 0;
		color: transparent;
	}
	.addressContainer{
		padding: 10px;
		box-sizing: border-box;
		.scrollView{
			height: 90vh;
			overflow: hidden;
			box-sizing: border-box;
			border-radius: 4px;
			.scrollViewScroll{
				scrollbar-width: none;
				.items{
					background-color: #fff;
					padding: 10px;
					border-radius: 4px;
					margin-bottom: 10px;
					.text{
						font-size: 14px;
						font-weight: bold;
						margin-bottom: 6px;
					}
					.editBtn{
						display: flex;
						font-size: 12px;
						justify-content: flex-end;
						.remove{
							color: #989898;
							margin-left: 10px;
						}
					}
				}
			}
			.scrollViewScroll::-webkit-scrollbar{
				display: none;
			}
		}
		.addAddress{
			height: calc(10vh - 20px);
			background-color: #fff;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 4px;
			.addButton{
				width: 90%;
				background-color: #2c6cf4;
				color: #fff;
				text-align: center;
				padding: 10px 30px;
				border-radius: 6px;
				box-sizing: border-box;
				font-size: 12px;
			}
		}
	}
</style>